<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 14/08/2017
 * Time: 17:34
 */

namespace common\models;

/**
 * 充值相关
 * Class MoneyDao
 * @package common\models
 */
class RechargeDaos
{
    /**
     * 充值历史
     *
     * @param int $status
     * @param int $pid
     * @param int $pLevel
     * @param int $uid
     * @param int $start
     * @param int $end
     * @param int $pageNo
     * @param int $pageSize
     * @param array $orderByArr
     * @return array
     */
    public static function history(
        $status = -1,
        $uid = -1,
        $start = -1,
        $end = -1,
        $pageNo = 1,
        $pageSize = 15,
        $orderByArr = ['pay_start_time' => 'desc']
    ) {
        $db = \Yii::$app->db;

        $sql_count = "SELECT  COUNT(1) count,SUM(`money`) money FROM `money_recharge` a ";
        $sql_lists = "SELECT 
                        a.`id`,
                        a.`resp_code`,
                        a.`userID`,
                        a.`recharge_sn`,
                        a.`money`,
                        a.`pay_start_time`,
                        a.`status`,
                        a.`type`,
                        a.`recharge_sn`,
                        b.`loginName`,
                        b.`accountName`,
                        b.`alias`,
                        c.`name` direction
                      FROM  `money_recharge` a ";

        $sql_where = '';
        $sql_join = "LEFT JOIN `user` b ON b.`userID` = a.`userID` 
                     LEFT JOIN `money_change_direction` c ON c.`id` = a.`direction`
        ";
        $sql_orderBy = '';
        $sql_limit = " LIMIT " . ($pageNo - 1) * $pageSize . "," . $pageSize;

        $tmpWhere = [];
        $bind_arr = [];

        if ($uid != -1) {
            $tmpWhere[] = 'a.`userID` = :uid';
            $bind_arr[':uid'] = $uid;
        }

        if ($start != -1) {
            $tmpWhere[] = 'a.`pay_start_time` >= :time_start';
            $bind_arr[':time_start'] = $start;
        }

        if ($end != -1) {
            //结束日期包含当天
            $tmpWhere[] = 'a.`pay_start_time` < :time_end';
            $bind_arr[':time_end'] = $end;
        }

        if ($status != -1) {
            //充值状态
            $tmpWhere[] = 'a.`status` = :status';
            $bind_arr[':status'] = $status;
        }

        if (!empty($tmpWhere)) {
            $sql_where = 'WHERE ' . implode(' AND ', $tmpWhere);
        }

        $tmpOrderByArr = [];
        if (!empty($orderByArr)) {
            $sql_orderBy = " ORDER BY ";
            foreach ($orderByArr as $k => $v) {
                $tmpOrderByArr[] = "a.`" . $k . "` " . $v;
            }
            $sql_orderBy .= implode($tmpOrderByArr, ' , ');
        }
        $tmp = $db->createCommand($sql_count . $sql_join . $sql_where, $bind_arr)->queryOne();

//        var_dump($sql_lists);
//        var_dump($sql_join);
//        var_dump($sql_where);
//        var_dump($sql_orderBy);
//        var_dump($sql_limit);
//        var_dump($bind_arr);
//        exit();
        $lists = $db->createCommand(
            $sql_lists . $sql_join . $sql_where . $sql_orderBy . $sql_limit,
            $bind_arr
        )->queryAll();
        return [
            'total' => $tmp['count'] ? $tmp['count'] : 0,
            'sumMoney' => $tmp['money'] ? $tmp['money'] : 0,
            'lists' => $lists
        ];
    }
}
